//
//  AppDelegate.swift
//  weibo
//
//  Created by 林帆 on 2017/3/10.
//  Copyright © 2017年 林帆. All rights reserved.
// 12

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        // #available 检测 设备版本。如果 10.0以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options:[.alert, .badge, .carPlay, .sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // 10.0以下
            // 取得用户授权显示通知【上方的提示条/声音/BadgeNumber】
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(notifySettings)
        }
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return .portrait
//        portrait  肖像；描写；半身雕塑像
//        landscape  风景；风景画；景色
    }
   
}

extension AppDelegate {
    fileprivate func loadAppInfo() {
         // 1. 模拟异步
        DispatchQueue.global().async {
            // 1. url
            let url = Bundle.main.url(forResource: "demo.json", withExtension: nil)
            // 2. data
            let data = NSData(contentsOf: url!)
            // 3. 写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("demo.json")
            data?.write(toFile: jsonPath, atomically: true)
            
        }
    }
}







